package com.sinothk.image.selector.adaper;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sinothk.image.selector.bean.MediaBean;
import com.sinothk.image.selector.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by donglua on 15/6/21.
 */
public class PhotoShowAdapter extends PagerAdapter {

    public interface VideoClickListener {
        void onClickListener(View view, int position, MediaBean mediaBean);
    }

    public VideoClickListener videoClickListener;

    private List<MediaBean> paths = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private boolean isNeedCache;

    public PhotoShowAdapter(Context mContext, List<MediaBean> paths, boolean isNeedCache) {
        this.mContext = mContext;
        this.isNeedCache = isNeedCache;
        this.paths = paths;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setVideoClickListener(VideoClickListener videoClickListener) {
        this.videoClickListener = videoClickListener;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View itemView = mLayoutInflater.inflate(R.layout.ms_item_preview, container, false);
        PhotoView imageView = (PhotoView) itemView.findViewById(R.id.iv_pager);

        RelativeLayout videoLayout = itemView.findViewById(R.id.videoLayout);
        ImageView videoView = itemView.findViewById(R.id.videoView);

        final MediaBean mediaBean = paths.get(position);

        if (mediaBean.isLocalIs()) {
            if (MediaBean.TYPE_VIDEO.equals(mediaBean.getType()) || MediaBean.TYPE_AUDIO.equals(mediaBean.getType())) {
                imageView.setVisibility(View.GONE);
                videoLayout.setVisibility(View.VISIBLE);

                ImageView videoViewBtn = itemView.findViewById(R.id.videoViewBtn);

                Glide.with(mContext)
                        .load(mediaBean.getResCover())
                        .placeholder(R.mipmap.default_error)
                        .error(R.mipmap.default_error)
                        .into(videoView);

                if (videoClickListener != null) {
                    videoViewBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            videoClickListener.onClickListener(v, position, mediaBean);
                        }
                    });
                }
            } else {
                imageView.setVisibility(View.VISIBLE);
                videoLayout.setVisibility(View.GONE);

                final Uri uri = Uri.fromFile(new File(mediaBean.getFilePath()));

                if (isNeedCache) {
                    Glide.with(mContext)
                            .load(uri)
                            .placeholder(R.mipmap.default_error)
                            .error(R.mipmap.default_error)
                            .into(imageView);
                } else {
                    Glide.with(mContext)
                            .load(uri)
                            .placeholder(R.mipmap.default_error)
                            .error(R.mipmap.default_error)
                            .skipMemoryCache(true)                      //禁止Glide内存缓存
                            .diskCacheStrategy(DiskCacheStrategy.NONE)  //不缓存资源
                            .into(imageView);
                }

                imageView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(View view, float v, float v1) {
                        videoClickListener.onClickListener(view, position, mediaBean);
                    }
                });
            }
        } else {
            if (MediaBean.TYPE_VIDEO.equals(mediaBean.getType()) || MediaBean.TYPE_AUDIO.equals(mediaBean.getType())) {
                imageView.setVisibility(View.GONE);
                videoLayout.setVisibility(View.VISIBLE);

                ImageView videoViewBtn = itemView.findViewById(R.id.videoViewBtn);

                Glide.with(mContext)
                        .load(mediaBean.getUrlCover())
                        .placeholder(R.mipmap.default_error)
                        .error(R.mipmap.default_error)
                        .into(videoView);

                if (videoClickListener != null) {
                    videoViewBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            videoClickListener.onClickListener(v, position, mediaBean);
                        }
                    });
                }
            } else {
                imageView.setVisibility(View.VISIBLE);
                videoLayout.setVisibility(View.GONE);

                final Uri uri = Uri.parse(mediaBean.getUrl());

                if (isNeedCache) {
                    Glide.with(mContext)
                            .load(uri)
                            .placeholder(R.mipmap.default_error)
                            .error(R.mipmap.default_error)
                            .into(imageView);
                } else {
                    Glide.with(mContext)
                            .load(uri)
                            .placeholder(R.mipmap.default_error)
                            .error(R.mipmap.default_error)
                            .skipMemoryCache(true)                      //禁止Glide内存缓存
                            .diskCacheStrategy(DiskCacheStrategy.NONE)  //不缓存资源
                            .into(imageView);
                }

                imageView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(View view, float v, float v1) {
                        videoClickListener.onClickListener(view, position, mediaBean);
                    }
                });
            }
        }

        container.addView(itemView);

        return itemView;
    }


    @Override
    public int getCount() {
        return paths.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
